home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.10 / shader.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-29  |  2.4 KB  |  100 lines

  1. #include "shader.h"
  2.  
  3. SHADER::SHADER()
  4. {
  5.     m_pPixelShader = NULL;
  6.     m_pVertexShader = NULL;
  7.     m_pConstantTable = NULL;
  8. }
  9.  
  10. SHADER::~SHADER()
  11. {
  12.     if(m_pPixelShader != NULL)
  13.     {
  14.         m_pPixelShader->Release();
  15.         m_pPixelShader = NULL;
  16.     }
  17.  
  18.     if(m_pVertexShader != NULL)
  19.     {
  20.         m_pVertexShader->Release();
  21.         m_pVertexShader = NULL;
  22.     }
  23. }
  24.  
  25. void SHADER::Init(IDirect3DDevice9 *Dev, const char fName[], int typ)
  26. {
  27.     m_pDevice = Dev;
  28.     m_type = typ;
  29.     if(m_pDevice == NULL)return;
  30.  
  31.     // Assemble and set the pixel or vertex shader     
  32.     HRESULT hRes;
  33.     LPD3DXBUFFER Code = NULL;
  34.     LPD3DXBUFFER ErrorMsgs = NULL;
  35.  
  36.     if(m_type == PIXEL_SHADER)
  37.         hRes = D3DXCompileShaderFromFile(fName, NULL, NULL, "Main", "ps_2_0", D3DXSHADER_DEBUG, &Code, &ErrorMsgs, &m_pConstantTable);
  38.     else hRes = D3DXCompileShaderFromFile(fName, NULL, NULL, "Main", "vs_2_0", D3DXSHADER_DEBUG, &Code, &ErrorMsgs, &m_pConstantTable);
  39.  
  40.     if((FAILED(hRes)) && (ErrorMsgs != NULL))        //If failed
  41.     {
  42.         if(m_type == PIXEL_SHADER)
  43.             debug.Print("Couldnt compile the Pixel Shader");
  44.         else debug.Print("Couldnt compile the Vertex Shader");
  45.  
  46.         debug.Print((char*)ErrorMsgs->GetBufferPointer());
  47.         return;
  48.     }
  49.  
  50.     if(m_type == PIXEL_SHADER)
  51.         hRes = m_pDevice->CreatePixelShader((DWORD*)Code->GetBufferPointer(), &m_pPixelShader);
  52.     else hRes = m_pDevice->CreateVertexShader((DWORD*)Code->GetBufferPointer(), &m_pVertexShader);
  53.  
  54.     if(FAILED(hRes))
  55.     {
  56.         if(m_type == PIXEL_SHADER)
  57.             debug.Print("Couldnt Create the Pixel Shader");
  58.         else debug.Print("Couldnt Create the Vertex Shader");
  59.         exit(0);
  60.     }
  61. }
  62.  
  63. void SHADER::Begin()
  64. {
  65.     if(m_type == PIXEL_SHADER)
  66.         HRESULT hRes = m_pDevice->SetPixelShader(m_pPixelShader);
  67.     else HRESULT hRes = m_pDevice->SetVertexShader(m_pVertexShader);
  68. }
  69.  
  70. void SHADER::End()
  71. {
  72.     if(m_type == PIXEL_SHADER)
  73.         HRESULT hRes = m_pDevice->SetPixelShader(NULL);
  74.     else HRESULT hRes = m_pDevice->SetVertexShader(NULL);
  75. }
  76.  
  77. D3DXHANDLE SHADER::GetConstant(char name[])
  78. {
  79.     return m_pConstantTable->GetConstantByName(NULL, name);
  80. }
  81.  
  82. void SHADER::SetFloat(D3DXHANDLE h, float f)
  83. {
  84.     m_pConstantTable->SetFloat(m_pDevice, h, f);
  85. }
  86.  
  87. void SHADER::SetVector3(D3DXHANDLE h, D3DXVECTOR3 v)
  88. {
  89.     m_pConstantTable->SetValue(m_pDevice, h, &v, sizeof(D3DXVECTOR3));
  90. }
  91.  
  92. void SHADER::SetVector4(D3DXHANDLE h, D3DXVECTOR4 v)
  93. {
  94.     m_pConstantTable->SetVector(m_pDevice, h, &v);
  95. }
  96.  
  97. void SHADER::SetMatrix(D3DXHANDLE h, D3DXMATRIX m)
  98. {
  99.     m_pConstantTable->SetMatrix(m_pDevice, h, &m);
  100. }